package com.book.service.impl;

import com.book.dao.admin.AdminDao;
import com.book.dao.admin.impl.AdminDaoImpl;
import com.book.dao.book.BookDao;
import com.book.dao.book.impl.BookDaoImpl;
import com.book.dao.reader.ReaderDao;
import com.book.dao.reader.impl.ReaderDaoImpl;
import com.book.pojo.Admin;
import com.book.pojo.Book;
import com.book.pojo.Reader;
import com.book.service.AdminService;

import java.util.List;

public class AdminServiceImpl implements AdminService {
    private AdminDao adminDao = new AdminDaoImpl();
    private BookDao bookDao = new BookDaoImpl();
    private ReaderDao readerDao = new ReaderDaoImpl();

    @Override
    public Admin login(Admin admin) {
        Admin dbAdmin = adminDao.getAdminByUsername(admin.getUsername());
        if (dbAdmin != null && dbAdmin.getPassword().equals(admin.getPassword())) {
            return dbAdmin; // 登录成功
        }
        return null; // 登录失败
    }

    @Override
    public int addAdmin(Admin admin) {
        return adminDao.addAdmin(admin);
    }

    @Override
    public Admin findAdminByUsername(String username) {
        return adminDao.getAdminByUsername(username);
    }

    @Override
    public boolean addBook(Book book) {
        int result = bookDao.addBook(book);

        // 根据插入的结果返回操作是否成功
        return result > 0;  // 如果插入了至少
    }

    @Override
    public boolean deleteBook(int id) {
        int result = bookDao.deleteBook(id);
        // 根据删除的结果返回操作是否成功
        return result > 0;  // 如果删除了至少
    }

    @Override
    public boolean updateBook(Book book) {
        int result = bookDao.updateBook(book);
        // 根据删除的结果返回操作是否成功
        return result > 0;  // 如果删除了至少
    }

    @Override
    public List<Book> findAllBooks() {
        List<Book> allBooks = bookDao.findAllBooks();
        return allBooks;
    }

    @Override
    public boolean addReader(Reader reader) {
        int result = readerDao.addReader(reader);
        // 根据删除的结果返回操作是否成功
        return result > 0;  // 如果删除了至少
    }

    @Override
    public List<Reader> getAllReaders() {
        List<Reader>list = readerDao.getAllReaders();
        return list;
    }

    @Override
    public boolean deleteReader(int readerId) {
        int result = readerDao.deleteReader(readerId);
        return result > 0;
    }
}
